package com.wcl.nullmusic.ui.fragment;

import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;

import com.wcl.nullmusic.R;
import com.wcl.nullmusic.base.BaseFragment;
import com.wcl.nullmusic.mode.PlayMusic;
import com.wcl.nullmusic.ui.service.NullMusicService;
import com.wcl.nullmusic.view.LyricView;

import java.io.IOException;

import de.greenrobot.event.EventBus;

/**
 * Created by dllo on 16/1/25.
 */
public class LyricFragment extends BaseFragment {
    private LyricView lyricView;
    private String mp3Path;
    private int INTERVAL = 45;//歌词每行的间隔

    @Override
    protected int getLayout() {
        return R.layout.fragment_lrc;
    }

    @Override
    protected void getVaule() {

    }

    @Override
    protected void initView(View view) {
        EventBus.getDefault().register(this);
        lyricView = (LyricView) view.findViewById(R.id.lrc_lrc);

        mp3Path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/1.mp3";
    }

    @Override
    protected void initData() {

        SerchLrc();
        lyricView.setSIZEWORD(120);
        lyricView.SetTextSize();
        lyricView.SetTextSize();
        lyricView.setOffsetY(200);
        new Thread(new runable()).start();
    }

    @Override
    protected void listener() {

    }

    class runable implements Runnable {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {

                try {
                    Thread.sleep(100);
                    if (true) {
                        lyricView.setOffsetY(lyricView.getOffsetY() - lyricView.SpeedLrc());
                        lyricView.SelectIndex(currentPosition);
                        // seekBar.setProgress(mediaPlayer.getCurrentPosition());
                        mHandler.post(mUpdateResults);
                    }
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    Handler mHandler = new Handler();
    Runnable mUpdateResults = new Runnable() {
        public void run() {
            lyricView.invalidate(); // 更新视图
        }
    };

    public void SerchLrc() {
        String lrc = mp3Path;
        lrc = lrc.substring(0, lrc.length() - 4).trim() + ".lrc".trim();
        LyricView.read(lrc);
        lyricView.SetTextSize();
        lyricView.setOffsetY(350);
    }


    private int currentPosition;
    private boolean isPlaying;

    public void onEvent(PlayMusic playMusic) {
        // Log.i("i", "有接到的信息  lrc");
        int state = playMusic.getState();
        switch (state) {

            case NullMusicService.SEND_MUSIC_CURRENTPOSITION:

                currentPosition = playMusic.getCurrentPosition();
                Log.i("pp", "有接到播放的当前位置 " + currentPosition);
                break;
            case NullMusicService.SEND_MUSIC_ISPLAYING:

                //    isPlaying = playMusic.isCurrentIsPlaying();
                Log.i("io", "有接到的信息  isplaying 播放状态" + isPlaying);
                break;
//                if (isPlaying) {
//                    EventBus.getDefault().post(NullMusicService.OBTAIN_MUSIC_ALLINFO);
//
//        }
        }
    }

    public void onEvent(Integer progress) {
        lyricView.setOffsetY(220 - lyricView.SelectIndex(progress)
                * (lyricView.getSIZEWORD() + INTERVAL - 1));
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
